package com.app.twth.ui.home;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.viewbinding.ViewBinding;

import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.app.MyApplication;
import com.app.twth.base.BaseFragment;
import com.app.twth.databinding.FragmentHomeBinding;
import com.app.twth.interfaces.ViewOnClickListener;
import com.app.twth.ui.checkin.CheckinActivity;
import com.app.twth.ui.lpnphoto.LPNPhotoActivity;
import com.app.twth.ui.putaway.PutawayActivity;
import com.app.twth.ui.search.SearchActivity;
import com.app.twth.ui.tally.TallyActivity;
import com.app.twth.utils.CommonUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * ****************************************************************
 * 文件名称: FollowersFragment
 * 文件描述: 使用 Room 持久化存储列表数据
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class HomeFragment extends BaseFragment implements ViewOnClickListener {

    private FragmentHomeBinding binding;
    private HomeViewModel viewModel;
    protected boolean hasRequested;//在onResume中判断是否已经请求过数据。用于懒加载

    @Override
    protected ViewBinding getViewBinding() {
        binding = FragmentHomeBinding.inflate(getLayoutInflater());
        return binding;
    }

    @Override
    protected void initListener() {
//        viewModel.getFollowers(true);
    }

    @Override
    protected void initData() {
        viewModel = new ViewModelProvider(requireActivity(), new ViewModelProvider.AndroidViewModelFactory(MyApplication.getAppInstance())).get(HomeViewModel.class);
        binding.setViewModel(viewModel);
        binding.setViewOnClick(this);
//        recyclerAdapter = new HomeAdapter.AuthorizeAdapter(mContext, new ArrayList<>());
//        recyclerAdapter.setOnItemClickListener((v, position) -> {
//            List<Authorize> list = viewModel.mData.getValue();
//            if (list != null && list.size() > position) {
//                Authorize data = list.get(position);
////                startActivity(ProfileDetailActivity.getStartIntent(mContext, data.getLogin()));
//            }
//        });
//        binding.recyclerView.setAdapter(recyclerAdapter);
    }

    @Override
    protected void initViewObservable() {
        //根据请求调整UI
        viewModel.followersResultCode.observe(this, code -> {
            if (code.equals("0")) {
//                recyclerAdapter.setData(list);
//                recyclerAdapter.notifyDataSetChanged();
            } else {
                CommonUtil.toast(viewModel.followersResultMsg);
            }
        });
        //根据数据库刷新列表
        viewModel.mData.observe(this, list -> {
//            recyclerAdapter.setData(list);
//            recyclerAdapter.notifyDataSetChanged();
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!hasRequested) {
            hasRequested = true;
        }
    }

    @Override
    public void OnClick(View view) {
        Log.e("tag", "view.getId():" + view.getId());
        switch (view.getId()) {
            case R.id.layout_search://查询
                AppRouter.showFragmentActivity(mContext, SearchActivity.class);
                break;
            case R.id.tv_receiving://收货
                AppRouter.showFragmentActivity(mContext, CheckinActivity.class);
                break;
            case R.id.tv_tally://常规理货
                Bundle tallyBundle = new Bundle();
                tallyBundle.putInt("typ", 1);
                AppRouter.showFragmentActivity(mContext, TallyActivity.class, tallyBundle);
                break;
            case R.id.tv_damage://破损录入
                Bundle damageBundle = new Bundle();
                damageBundle.putInt("typ", 2);
                AppRouter.showFragmentActivity(mContext, TallyActivity.class, damageBundle);
                break;
            case R.id.tv_lpn_photo://LPN 图片
                AppRouter.showFragmentActivity(mContext, LPNPhotoActivity.class);
                break;
            case R.id.tv_putaway://上架
                AppRouter.showFragmentActivity(mContext, PutawayActivity.class);
                break;
            case R.id.tv_b2c_pick://拣货
//                AppRouter.showFragmentActivity(mContext, B2CPickActivity.class);
                break;
            case R.id.tv_b2c_trailer://装车
//                AppRouter.showFragmentActivity(mContext, B2CTrailerActivity.class);
                break;
            case R.id.tv_b2b_pick://b2b拣货
//                AppRouter.showFragmentActivity(mContext, B2BPickActivity.class);
                break;
            case R.id.tv_b2b_trailer://b2b出库
//                AppRouter.showFragmentActivity(mContext, B2BTrailerActivity.class);
                break;
            case R.id.tv_preprocess://退件预处理
//                AppRouter.showFragmentActivity(mContext, PreprocessTrackingActivity.class);
                break;
            case R.id.tv_finalprocess://二次退件
//                AppRouter.showFragmentActivity(mContext, FinalProcessActivity.class);
                break;
            case R.id.tv_returns_putaway://退件上架
//                AppRouter.showFragmentActivity(mContext, ReturnsPutatayActivity.class);
                break;
            case R.id.tv_returns_trailer://退件装车
//                AppRouter.showFragmentActivity(mContext, ReturnsTrailerActivity.class);
                break;
            case R.id.tv_cyclecount://盘点
//                AppRouter.showFragmentActivity(mContext, CycleCountActivity.class);
                break;
            case R.id.tv_transfer://移库
//                AppRouter.showFragmentActivity(mContext, TransferActivity.class);
                break;
            case R.id.tv_adjust://调整库存
//                AppRouter.showFragmentActivity(mContext, AdjustActivity.class);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThread(Object object) {
    }

    @Override
    public void onDestroy() {
        if (viewModel != null) {
            viewModel.dispose();
            viewModel = null;
        }
        super.onDestroy();
    }
}